/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   Module Name          : com.yaodian100.crm.workflow.web.WorkFlowAction
   Module Description   :

   Date Created      : 2009/12/8
   Original Author   : andy.chuang
   Team              : yaodian100
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   MODIFICATION HISTORY
   ------------------------------------------------------------------------------
   Date Modified       Modified by       Comments
   ------------------------------------------------------------------------------
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
package com.yaodian100.crm.workflow.web;

import java.util.Date;

import javax.annotation.Resource;

import org.springframework.transaction.annotation.Transactional;

import com.yaodian100.core.admin.utils.AdminHelper;
import com.yaodian100.core.common.service.BaseManager;
import com.yaodian100.core.form.entity.Attribute;
import com.yaodian100.crm.workflow.entity.WorkFlow;
import com.yaodian100.crm.workflow.entity.WorkFlowNode;
import com.yaodian100.crm.workflow.entity.WorkFlowNodeAct;

/**
 * @author andy.chuang
 * 
 */
@Transactional
public class WorkFlowAction {
	private String iamlazy;
	@Resource
	private BaseManager baseMgr;

	// for workflow
	private String name;
	private String description;
	private String mustReturnMaster;

	// for workflow node
	private String id;
	private String parentId;
	private String responserId;
	private String workFlowName;
	private String shortDesc;
	private String allowBackward;
	private String needApprove;
	private String approver;
	private String callOut;
	private String responseGroup;
	private String approveGroup;
	private String beApprovedGroup;
	private String AJAX;

	/**
	 * @return the beApprovedGroup
	 */
	public String getBeApprovedGroup() {
		return beApprovedGroup;
	}

	/**
	 * @param beApprovedGroup the beApprovedGroup to set
	 */
	public void setBeApprovedGroup(String beApprovedGroup) {
		this.beApprovedGroup = beApprovedGroup;
	}

	/**
	 * @return the responseGroup
	 */
	public String getResponseGroup() {
		return responseGroup;
	}

	/**
	 * @param responseGroup the responseGroup to set
	 */
	public void setResponseGroup(String responseGroup) {
		this.responseGroup = responseGroup;
	}

	/**
	 * @return the approveGroup
	 */
	public String getApproveGroup() {
		return approveGroup;
	}

	/**
	 * @param approveGroup the approveGroup to set
	 */
	public void setApproveGroup(String approveGroup) {
		this.approveGroup = approveGroup;
	}

	/**
	 * @return the callOut
	 */
	public String getCallOut() {
		return callOut;
	}

	/**
	 * @param callOut the callOut to set
	 */
	public void setCallOut(String callOut) {
		this.callOut = callOut;
	}

	/**
	 * @return the needApprove
	 */
	public String getNeedApprove() {
		return needApprove;
	}

	/**
	 * @param needApprove the needApprove to set
	 */
	public void setNeedApprove(String needApprove) {
		this.needApprove = needApprove;
	}

	/**
	 * @return the approver
	 */
	public String getApprover() {
		return approver;
	}

	/**
	 * @param approver the approver to set
	 */
	public void setApprover(String approver) {
		this.approver = approver;
	}

	/**
	 * @return the mustReturnMaster
	 */
	public String getMustReturnMaster() {
		return mustReturnMaster;
	}

	/**
	 * @param mustReturnMaster the mustReturnMaster to set
	 */
	public void setMustReturnMaster(String mustReturnMaster) {
		this.mustReturnMaster = mustReturnMaster;
	}

	/**
	 * @return the allowBackward
	 */
	public String getAllowBackward() {
		return allowBackward;
	}

	/**
	 * @param allowBackward the allowBackward to set
	 */
	public void setAllowBackward(String allowBackward) {
		this.allowBackward = allowBackward;
	}

	/**
	 * @return the shortDesc
	 */
	public String getShortDesc() {
		return shortDesc;
	}

	/**
	 * @param shortDesc the shortDesc to set
	 */
	public void setShortDesc(String shortDesc) {
		this.shortDesc = shortDesc;
	}

	/**
	 * @return the id
	 */
	public String getId() {
		return id;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(String id) {
		this.id = id;
	}

	/**
	 * @return the parentId
	 */
	public String getParentId() {
		return parentId;
	}

	/**
	 * @param parentId the parentId to set
	 */
	public void setParentId(String parentId) {
		this.parentId = parentId;
	}

	/**
	 * @return the responserId
	 */
	public String getResponserId() {
		return responserId;
	}

	/**
	 * @param responserId the responserId to set
	 */
	public void setResponserId(String responserId) {
		this.responserId = responserId;
	}

	/**
	 * @return the workFlowName
	 */
	public String getWorkFlowName() {
		return workFlowName;
	}

	/**
	 * @param workFlowName the workFlowName to set
	 */
	public void setWorkFlowName(String workFlowName) {
		this.workFlowName = workFlowName;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * @param description the description to set
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * @return the iamlazy
	 */
	public String getIamlazy() {
		return iamlazy;
	}

	/**
	 * @param iamlazy the iamlazy to set
	 */
	public void setIamlazy(String iamlazy) {
		this.iamlazy = iamlazy;
	}

	public String page() {
		if (AJAX != null && AJAX.equals("true")) {
			return iamlazy + "_ajax";
		} else {
			return iamlazy;
		}
	}

	private String returnPage(String orgPage) {
		if (AJAX != null && AJAX.equals("true")) {
			return orgPage + "_ajax";
		} else {
			return orgPage;
		}
	}

	public String updateFlow() {
		WorkFlow wo = (WorkFlow) baseMgr.get(WorkFlow.class, name);
		Date currentDate = new Date();
		boolean isCreate = false;
		if (wo == null) {
			isCreate = true;
			wo = new WorkFlow();
			wo.setCreateDate(currentDate);
			wo.setCreateUser(AdminHelper.getUser().getUsername());
			wo.setName(name);
		}

		wo.setDescription(description);
		wo.setMustReturnMaster(Boolean.valueOf(mustReturnMaster));
		wo.setModifyUser(AdminHelper.getUser().getUsername());
		if (isCreate) {
			baseMgr.save(wo);
		} else {
			baseMgr.update(wo);
		}

		return returnPage("updateFlow");
	}

	private String[] nodeAct;

	/**
	 * @return the nodeAct
	 */
	public String[] getNodeAct() {
		return nodeAct;
	}

	/**
	 * @param nodeAct the nodeAct to set
	 */
	public void setNodeAct(String[] nodeAct) {
		this.nodeAct = nodeAct;
	}

	public String update() {
		WorkFlowNode wfn = null;
		Date currentDate = new Date();
		boolean isExist = false;
		if (id == null || id.length() == 0) {
			// create
			wfn = new WorkFlowNode();
			wfn.setCreateDate(currentDate);
			wfn.setCreateUser(AdminHelper.getUser().getUsername());

		} else {
			wfn = (WorkFlowNode) baseMgr.get(WorkFlowNode.class, id);
			isExist = true;
		}
		if (parentId != null && parentId.length() > 0) {
			WorkFlowNode wfnf = new WorkFlowNode();
			wfnf.setId(parentId);
			wfn.setParent(wfnf);
		}
		wfn.setModifyUser(AdminHelper.getUser().getUsername());
		wfn.setResponser(responserId);
		wfn.setResponseGroup(responseGroup);
		wfn.setApproveGroup(approveGroup);
		wfn.setBeApprovedGroup(beApprovedGroup);
		WorkFlow wo = new WorkFlow();
		if (callOut != null && callOut.length() > 0) {
			wfn.setCallOut(Boolean.valueOf(callOut));
		}
		wo.setName(workFlowName);
		wfn.setWorkFlow(wo);
		wfn.setShortDesc(shortDesc);
		wfn.setNeedApprove(Boolean.valueOf(needApprove));
		wfn.setApprover(approver);
		if (allowBackward != null && allowBackward.length() > 0) {
			wfn.setAllowBackward(new Boolean(allowBackward));
		}

		baseMgr.saveOrUpdate(wfn);
		// node act
		if (isExist) {
			// clear act
			baseMgr.executeHql("delete from WorkFlowNodeAct where node.id=?", new Object[] { id });

		}
		if (nodeAct != null && nodeAct.length > 0) {
			for (String attrId : nodeAct) {
				WorkFlowNodeAct wa = new WorkFlowNodeAct();
				wa.setNode(wfn);
				Attribute attr = (Attribute) baseMgr.get(Attribute.class, attrId);
				wa.setAttr(attr);
				baseMgr.save(wa);
			}
		}
		return returnPage("update");
	}

	public String deleteNode() {
		WorkFlowNode wfn = new WorkFlowNode();
		wfn.setId(id);
		baseMgr.delete(wfn);
		return returnPage("deleteNode");
	}

	public String deleteFlow() {
		WorkFlow wf = new WorkFlow();
		wf.setName(name);
		baseMgr.delete(wf);
		return returnPage("deleteFlow");
	}

	/**
	 * @return the aJAX
	 */
	public String getAJAX() {
		return AJAX;
	}

	/**
	 * @param aJAX the aJAX to set
	 */
	public void setAJAX(String aJAX) {
		AJAX = aJAX;
	}
}
